package com.hckj.jianzhijia.model;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVFile;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.SaveCallback;
import com.hckj.jianzhijia.utils.Data;

/**
 * Created by 赵奇隆 on 2015-10-18.
 *
 * 没有用子类化，子类化扩展不方便，没办法自定义太多东西
 */
public class CompanyUser {

    private AVObject userInfo;
    private AVUser avUser;
    private String objectId;
    private String name;
    private String password;
    private String email;
    private String phone;
    private int userType;
    private AVFile logo;//初始情况是空
    private String description;//这个是设置时候才有
    private boolean isIdentified = false;//注册时候默认为false
    private String companyAddress;//默认的精确地址，暂时没有市区地理位置信息
    private double latitude;
    private double longitude;
    private int provinceId;
    private int cityId;
    private int regionId;
    private boolean emailVerified = false;
    private String provinceName = "";
    private String cityName = "";
    private String regionName = "";

    /**
     * 这个方法是用来获取用户信息并将其存储的
     * @param avUser
     */
    public CompanyUser(AVUser avUser) {

        this.userInfo = avUser.getAVObject("userInfo");
        this.avUser = avUser;
        this.companyAddress = userInfo.getString("companyAddress");
        this.description = userInfo.getString("description");
        this.email = avUser.getEmail();
        this.isIdentified = userInfo.getBoolean("isIdentified");
        this.latitude = userInfo.getDouble("latitude");
        this.longitude = userInfo.getDouble("longitude");
        this.cityId = userInfo.getInt("cityId");
        this.provinceId = userInfo.getInt("provinceId");
        this.regionId = userInfo.getInt("regionId");
        this.phone = avUser.getUsername();
        this.logo = userInfo.getAVFile("logo");
        this.objectId = avUser.getObjectId();
        this.name = avUser.getString("name");
        this.userType = avUser.getInt("userType");
        this.emailVerified = avUser.getBoolean("emailVerified");

    }


    /**
     * 这个方法是用来生成用户信息
     */
    public CompanyUser(){
        avUser = new AVUser();
        userInfo = new AVObject("companyUser");
    }

    /**
     * 这个方法用来获取保存好的user对象
     * @return
     */
    public AVUser registerAvUser() {//只能注册时候使用，其他情况下调用会出问题

        avUser.setEmail(email);
        avUser.setUsername(phone);
        avUser.setPassword(password);
        avUser.setMobilePhoneNumber(phone);
        avUser.put("userType", Data.USER_TYPE_COMPANY);
        avUser.put("name", name);
        userInfo.put("logo",logo);
        userInfo.put("description",description);
        userInfo.put("isIdentified",isIdentified);
        userInfo.put("companyAddress",companyAddress);
        userInfo.put("latitude",latitude);
        userInfo.put("longitude",longitude);
        userInfo.put("provinceId",provinceId);
        userInfo.put("cityId",cityId);
        userInfo.put("regionId",regionId);
        userInfo.put("phone",phone);
        //暂时不加
//        userInfo.put("provinceName",provinceName);
//        userInfo.put("cityName",cityName);
//        userInfo.put("regionName",regionName);
        avUser.put("userInfo",userInfo);
        return avUser;
    }

    public void logOut(){
        avUser.logOut();
    }

    public void setAvUser(AVUser avUser) {
        this.avUser = avUser;
    }

    public String getCompanyAddress() {
        return companyAddress;
    }

    public void setCompanyAddress(String companyAddress) {
        this.companyAddress = companyAddress;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public boolean isIdentified() {
        return isIdentified;
    }

    public void setIsIdentified(boolean isIdentified) {
        this.isIdentified = isIdentified;
    }

    public AVFile getLogo() {
        return logo;
    }

    public void setLogo(AVFile logo) {
        this.logo = logo;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getObjectId() {
        return objectId;
    }

    public void setObjectId(String objectId) {
        this.objectId = objectId;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getUserType() {
        return userType;
    }

    public void setUserType(int userType) {
        this.userType = userType;
    }

    public AVObject getUserInfo() {

//        userInfo.put("logo",logo);
//        userInfo.put("description",description);
//        userInfo.put("isIdentified",isIdentified);
//        userInfo.put("companyAddress",companyAddress);
//        userInfo.put("latitude",latitude);
//        userInfo.put("longitude",longitude);
//        userInfo.put("provinceId",provinceId);
//        userInfo.put("cityId",cityId);
//        userInfo.put("regionId",regionId);

        return userInfo;
    }

    public AVObject saveUserInfo(){
        userInfo.put("logo",logo);
        userInfo.put("description",description);
        userInfo.put("isIdentified",isIdentified);
        userInfo.put("companyAddress",companyAddress);
        userInfo.put("latitude",latitude);
        userInfo.put("longitude",longitude);
        userInfo.put("provinceId",provinceId);
        userInfo.put("cityId",cityId);
        userInfo.put("regionId",regionId);
//        userInfo.put("provinceName",provinceName);
//        userInfo.put("cityName",cityName);
//        userInfo.put("regionName",regionName);
        return userInfo;
    }

    public void setUserInfo(AVObject userInfo) {
        this.userInfo = userInfo;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public int getCityId() {
        return cityId;
    }

    public void setCityId(int cityId) {
        this.cityId = cityId;
    }

    public int getProvinceId() {
        return provinceId;
    }

    public void setProvinceId(int provinceId) {
        this.provinceId = provinceId;
    }

    public int getRegionId() {
        return regionId;
    }

    public void setRegionId(int regionId) {
        this.regionId = regionId;
    }

    public double getLatitude() {
        return latitude;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public double getLongitude() {
        return longitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public String getProvinceName() {
        return provinceName;
    }

    public void setProvinceName(String provinceName) {
        this.provinceName = provinceName;
    }

    public String getRegionName() {
        return regionName;
    }

    public void setRegionName(String regionName) {
        this.regionName = regionName;
    }

    public boolean isEmailVerified() {
        return emailVerified;
    }

    public void setEmailVerified(boolean emailVerified) {
        this.emailVerified = emailVerified;
    }

    public AVUser getAvUser() {//用于修改其他类型信息
        avUser.setEmail(email);
        avUser.setUsername(phone);
        avUser.setMobilePhoneNumber(phone);
        avUser.put("name",name);
        userInfo.put("logo",logo);
        userInfo.put("description",description);
        userInfo.put("isIdentified",isIdentified);
        userInfo.put("companyAddress",companyAddress);
        userInfo.put("latitude",latitude);
        userInfo.put("longitude",longitude);
        userInfo.put("provinceId",provinceId);
        userInfo.put("cityId",cityId);
        userInfo.put("regionId",regionId);
        avUser.put("userInfo",userInfo);
        return avUser;
    }
}
